# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2020/12/17 21:36
# @Author  : darknight
# @File    : parser.py
# @Software: PyCharm
import html

from core.parser import BaseParse
from settings import settings

QQ_PIC = "https://y.gtimg.cn/music/photo_new/T002R300x300M000{}.jpg"
QQ_MV_DEMON = 'https://mv.music.tc.qq.com/'
QQ_MUSIC_DEMON = 'https://isure.stream.qqmusic.qq.com/'


class Parser(BaseParse):

    @staticmethod
    def parser_recommend_songs_list(source, result) -> list:
        data = [dict(
            id=item['content_id'],
            pic=item['cover'],
            listen_num=item['listen_num'],
            title=item['title'],
            source=source,
        ) for item in result['recomPlaylist']['data']['v_hot']]
        return data[0: Parser.LIMIT]

    @staticmethod
    def parser_recommend_songs(source, result) -> list:
        result = result['new_song']['data']['songlist']
        data = [dict(
            id=item['id'],
            mid=item['mid'],
            songName=item['name'],
            singer=[singer['name'] for singer in item['singer']],
            vid=item['mv']['vid'],
            pic=QQ_PIC.format(item['album']['mid']),
            source=source,
        ) for item in result]
        return data[0:Parser.LIMIT]

    @staticmethod
    def parser_songs_list_by_category(source, result) -> list:
        result = result['data']['list']
        data = [dict(
            id=item['dissid'],
            pic=item['imgurl'],
            listen_num=item['listennum'],
            title=item['dissname'],
            source=source,
        ) for item in result]
        return data[0:Parser.LIMIT]

    @staticmethod
    def parser_songs_list_detail(source, result) -> dict:
        detail = result['cdlist'][0]
        songs = detail['songlist']
        data = dict(
            id=detail['disstid'],
            logo=detail['logo'],
            title=detail['dissname'],
            desc=detail['desc'],
            listen_num=detail['visitnum'],
            creator=detail['nickname'],
            source=source,
            songs=[
                dict(
                    id=song['id'],
                    mid=song['mid'],
                    songName=song['name'],
                    singer=[singer['name'] for singer in song['singer']],
                    vid=song['mv']['vid'],
                    pic=QQ_PIC.format(song['album']['mid']),
                    source=source,
                ) for song in songs
            ]
        )
        return data

    @staticmethod
    def parser_song_detail(source, result) -> dict:
        res = result['songinfo']['data']['track_info']
        data = dict(
            id=res['id'],
            mid=res['mid'],
            songName=res['name'],
            singer=[item['name'] for item in res['singer']],
            vid=res['mv']['vid'],
            pic=QQ_PIC.format(res['album']['mid']),
            duration=res['interval'] * 1000,
            source=source,
        )
        return data

    @staticmethod
    def parser_song_play_url(source, result) -> str:
        data = QQ_MUSIC_DEMON + result['req_0']['data']['midurlinfo'][0]['purl']
        return data

    @staticmethod
    def parser_song_lyric(source, result) -> str:
        data = html.unescape(result['lyric'])
        return data

    @staticmethod
    def parser_mv_play_url(source, result) -> list:
        data = [
            {settings.VIDEO_QUALITY[item['filetype']]: f"{QQ_MV_DEMON}{item['vkey']}/{item['cn']}?fname={item['vkey']}"}
            for item in list(result['getMvUrl']['data'].values())[0]['mp4'] if item['code'] == 0
        ]
        return data

    @staticmethod
    def parser_top_list(source, result) -> dict:
        detail = result['detail']['data']['data']
        songs = result['detail']['data']['songInfoList']
        data = dict(
            id=detail['topId'],
            logo=detail['mbHeadPicUrl'],
            title=detail['title'],
            desc=detail['intro'],
            listen_num=detail['listenNum'],
            source=source,
            songs=[dict(
                id=song['id'],
                mid=song['mid'],
                songName=song['name'],
                singer=[singer['name'] for singer in song['singer']],
                vid=song['mv']['vid'],
                pic=QQ_PIC.format(song['album']['mid']),
                source=source,
            ) for song in songs],
        )
        return data

    @staticmethod
    def parser_search_song(source, result) -> list:
        songs = result['data']['song']['list']
        data = [dict(
            id=song['id'],
            mid=song['mid'],
            songName=song['name'],
            singer=[item['name'] for item in song['singer']],
            vid=song['mv']['vid'],
            pic=QQ_PIC.format(song['album']['mid']),
            source=source,
        ) for song in songs]

        return data[0: Parser.LIMIT]
